package dataAccessLayer.factoryMySQL.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dataAccessLayer.abstractFactory.daos.DAOrecipesIngredients;
import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.visualObject.VOrecipeIngredient;

public class DAOrecipesIngredientsMySQL implements DAOrecipesIngredients 
{
	private DBManager dbManager;
	
	public DAOrecipesIngredientsMySQL(DBManager dbManager) 
	{
		this.dbManager = dbManager;
	}

	public boolean insertRecipeIngredient(VOrecipeIngredient vOrecipeIngredient) 
	{
		PreparedStatement preparedStatement = null;
		boolean success = true;

		Connection connection = dbManager.getConnection();

		try
		{
			preparedStatement = connection.prepareStatement("INSERT INTO recipesIngredients (recipe,ingredient,quantity) VALUES (?,?,?)");

			preparedStatement.setInt(1, vOrecipeIngredient.getRecipe());
			preparedStatement.setInt(2, vOrecipeIngredient.getIngredient());
			preparedStatement.setInt(3, vOrecipeIngredient.getQuantity());

			preparedStatement.execute();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			success = false;
		}
		finally
		{
			try
			{
				preparedStatement.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return success;
	}

	public boolean removeRecipeIngredient(VOrecipeIngredient vOrecipeIngredient) 
	{
		PreparedStatement preparedStatement = null;
		boolean success = true;
		Connection connection = dbManager.getConnection();
		
		try
		{
			preparedStatement = connection.prepareStatement("DELETE FROM recipesIngredients WHERE ingredient=? AND recipe=?");
			
			preparedStatement.setInt( 1 , vOrecipeIngredient.getIngredient());
			preparedStatement.setInt( 2 , vOrecipeIngredient.getRecipe());
			
			preparedStatement.execute();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			success = false;
		}
		finally
		{
			try
			{
				preparedStatement.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return success;
	}
	
	public ArrayList<VOrecipeIngredient> listRecipeIngredients(VOrecipeIngredient vOrecipeIngredient)
	{
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<VOrecipeIngredient> result = null;

		Connection connection = dbManager.getConnection();
		try
		{
			preparedStatement = connection.prepareStatement( "SELECT recipe,ingredient,quantity FROM recipesIngredients WHERE recipe=?" );
			preparedStatement.setInt(1, vOrecipeIngredient.getRecipe());
			
			resultSet = preparedStatement.executeQuery();

			result = new ArrayList<VOrecipeIngredient>();

			while( resultSet.next() )
			{
				VOrecipeIngredient recipeIngredient = new VOrecipeIngredient();
				recipeIngredient.setRecipe( resultSet.getInt(1) );
				recipeIngredient.setIngredient( resultSet.getInt(2) );
				recipeIngredient.setQuantity(resultSet.getInt(3));

				result.add( recipeIngredient);
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				resultSet.close();
				preparedStatement.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return result;
	}
	
}
